package com.springboot.demo.controller;

import com.springboot.demo.entity.User;
import com.springboot.demo.entity.UserDetailsImpl;
import com.springboot.demo.exception.BusinessException;
import com.springboot.demo.utils.Constant;
import com.springboot.demo.utils.ResponseResult;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * @author wubc683
 * @desc 处理客户端通过websocket+stomp+rabbitmq(代理）发来的消息
 * @date 2021-11-10 9:56
 */
@RestController
public class WebSocketController {
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    /**
     * 公共消息(要群发给客户端）
     *
     * @param accessor
     * @param msg
     * @return: void
     */
    @MessageMapping("/public")
    public void publicMessageHandle(StompHeaderAccessor accessor, String msg) {
        UsernamePasswordAuthenticationToken simpUser = (UsernamePasswordAuthenticationToken)
                accessor.getMessageHeaders().get("simpUser");
        if(simpUser == null){
            throw new BusinessException(ResponseResult.WEBSOCKET_ERROR);
        }
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) simpUser.getPrincipal();
        User implUser = userDetailsImpl.getUser();
        simpMessagingTemplate.convertAndSend(Constant.WEBSOCKET_PUBLIC_ROOTINGKEY, msg);
    }

    /**
     * 私人消息(单发给客户端）
     *
     * @param username
     * @param msg
     * @return: void
     */
    @MessageMapping("/private/{username}")
    public void privateMessageHandle(@DestinationVariable String username, StompHeaderAccessor accessor,String msg) {
        UsernamePasswordAuthenticationToken simpUser = (UsernamePasswordAuthenticationToken)
                accessor.getMessageHeaders().get("simpUser");
        if(simpUser == null){
            throw new BusinessException(ResponseResult.WEBSOCKET_ERROR);
        }
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) simpUser.getPrincipal();
        User implUser = userDetailsImpl.getUser();
        if(implUser == null){
            throw new BusinessException(ResponseResult.USER_NOT_EXISTED);
        }
        simpMessagingTemplate.convertAndSendToUser(username, Constant.WEBSOCKET_PRIVATE_ROOTINGKEY, "服务器" + msg);
    }

//    /**
//     * @param
//     * @return: java.lang.String
//     */
//    @GetMapping("/sendToSystem")
//    public String sendToSystem() {
//        Map<String, Object> map = new HashMap<>();
//        map.put("msg", "系统消息来了");
//rabbitTemplate.convertAndSend("non-existent-exchange", "TestDirectRouting", map,message -> {
//        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
//        System.out.println("sendmessage22222"+message);
//        return message;
//    },new CorrelationData(UUID.randomUUID().toString()));
//        return "ok";
//    }

}
